const { validationResult } = require('express-validator');

// 格式化验证错误信息
const formatErrors = (errors) => {
    return errors.array().map(error => ({
        field: error.param,
        message: error.msg,
        value: error.value
    }));
};

const validateRequest = (req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.json({
            code: 400,
            message: '请求参数验证失败',
            errors: formatErrors(errors)
        });
    }
    next();
};

module.exports = {
    validateRequest
};